package com.qingzhuge.scheduler.service.impl;

import com.qingzhuge.scheduler.dao.SysTaskScheduledDao;
import com.qingzhuge.scheduler.dto.request.TaskJobKey;
import com.qingzhuge.scheduler.dto.respone.SysTaskScheduledResponseDto;
import com.qingzhuge.scheduler.entity.SysTaskScheduled;
import com.qingzhuge.scheduler.service.ISchedulerService;
import com.qingzhuge.scheduler.service.ISysTaskScheduledService;
import com.qingzhuge.service.BasePkLongServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : zero.xiao
 * @description :
 * @date :2019-05-17 15:03
 * @modified :
 */
@Service
public class SysTaskScheduledServiceImpl extends BasePkLongServiceImpl<SysTaskScheduledDao, SysTaskScheduled>
        implements ISysTaskScheduledService {
    @Resource
    private ISchedulerService schedulerService;


    @Override
    public void updateStatus(Long id, Integer status) {
        baseDao.updateStatus(id,status);
    }

    @Override
    public SysTaskScheduled findTaskJobKeyById(Long id) {
        if (null == id) {
            return null;
        }
        return baseDao.findTaskJobKeyById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public SysTaskScheduled save(SysTaskScheduled entity) {
        SysTaskScheduled scheduled = super.save(entity);
        schedulerService.task(scheduled);
        return scheduled;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SysTaskScheduled scheduled) {
        if (null != scheduled && !scheduled.isNew()) {
            TaskJobKey jobKey = schedulerService.getTaskJobKey(scheduled);
            if (null == jobKey) {
                scheduled = findTaskJobKeyById(scheduled.getId());
                jobKey = schedulerService.getTaskJobKey(scheduled);
            }
            super.delete(scheduled);
            schedulerService.delTask(jobKey);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        SysTaskScheduled scheduled = findTaskJobKeyById(id);
        if (null != scheduled) {
            this.delete(scheduled);
        }
    }


    /**
     * 获取所有作业
     */
    @Override
    public List<SysTaskScheduledResponseDto> getAllTaskDetail() {
        return schedulerService.getAllTaskDetail();
    }

    /**
     * 新建、修改任务
     */
    @Override
    public void task(SysTaskScheduled taskScheduled) {
        save(taskScheduled);
    }

    /**
     * 暂停所有触发器
     */
    @Override
    public void pauseAllTrigger() {
        schedulerService.pauseAllTrigger();
    }

    /**
     * 启动所有触发器
     */
    @Override
    public void startAllTrigger() {
        schedulerService.startAllTrigger();
    }

    /**
     * 执行作业
     */
    @Override
    public void execTask(Long id) {
        SysTaskScheduled scheduled = findTaskJobKeyById(id);
        if (null != scheduled) {
            TaskJobKey jobKey = schedulerService.getTaskJobKey(scheduled);
            schedulerService.execTask(jobKey);
        }
    }

    /**
     * 恢复作业
     */
    @Override
    public void openTask(Long id) {
        SysTaskScheduled scheduled = findTaskJobKeyById(id);
        if (null != scheduled) {
            TaskJobKey jobKey = schedulerService.getTaskJobKey(scheduled);
            schedulerService.openTask(jobKey);
        }
    }

    /**
     * 暂停作业
     */
    @Override
    public void stopTask(Long id) {
        SysTaskScheduled scheduled = findTaskJobKeyById(id);
        if (null != scheduled) {
            TaskJobKey jobKey = schedulerService.getTaskJobKey(scheduled);
            schedulerService.stopTask(jobKey);
        }
    }

    /**
     * 删除作业
     */
    @Override
    public void delTask(Long id) {
        SysTaskScheduled scheduled = findTaskJobKeyById(id);
        if (null != scheduled) {
            TaskJobKey jobKey = schedulerService.getTaskJobKey(scheduled);
            schedulerService.delTask(jobKey);
        }
    }

    @Override
    public void delTask(String taskName,String taskGroup) {
        if (StringUtils.isNotEmpty(taskName)&&StringUtils.isNotEmpty(taskGroup)) {
            schedulerService.delTask(new TaskJobKey(taskName, taskGroup));
        }
    }
}
